package nl.changer.audiowifedemo_ohos.slice;

import nl.changer.audiowife.AudioWife;
import nl.changer.audiowife.HiLogHelper;
import nl.changer.audiowifedemo_ohos.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.media.player.Player;
import ohos.utils.net.Uri;

import java.io.*;

import static nl.changer.audiowifedemo_ohos.MainAbility.TEST_AUDIO_FILE_PATH;

public class MainAbilitySlice extends BaseAbilitySlice {
    private static final String TAG = MainAbilitySlice.class.getSimpleName();

    private static final int INTENT_PICK_AUDIO = 1;

    private Context mContext;

    private Component mPlayMedia;
    private Component mPauseMedia;
    private Slider mMediaSeekBar;
    private Text mRunTime;
    private Text mTotalTime;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_awd_main);

        mContext = this;

        Component pickAudio = findComponentById(ResourceTable.Id_pickAudio);

        pickAudio.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                pickAudio();
            }
        });

        // initialize the player controls
        mPlayMedia = findComponentById(ResourceTable.Id_play);
        mPauseMedia = findComponentById(ResourceTable.Id_pause);
        mMediaSeekBar = (Slider) findComponentById(ResourceTable.Id_media_seekbar);
        mRunTime = (Text) findComponentById(ResourceTable.Id_run_time);
        mTotalTime = (Text) findComponentById(ResourceTable.Id_total_time);

        mPlayMedia.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mUri == null) {
                    ToastHelper.init(mContext).setMessage("Pick an audio file before playing").show();
                }

            }
        });

        findComponentById(ResourceTable.Id_default_player).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();

                Operation operation = new Intent.OperationBuilder()
                        .withBundleName("nl.changer.audiowifedemo_ohos")
                        .withAbilityName("nl.changer.audiowifedemo_ohos.DefaultPlayerAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
    }

    private void pickAudio() {
        //todo 跳转到文件管理器，选择音频文件,当前ohos不支持该功能
        //模拟选择了音频文件
        File file = new File(TEST_AUDIO_FILE_PATH);
        Uri uri = Uri.getUriFromFile(file);
        initAudio(uri);
        new ToastDialog(this).setText("Pick Audio Success").show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (resultCode == 0) {
            if (requestCode == INTENT_PICK_AUDIO) {
                Uri uri = resultData.getUri();

                initAudio(uri);
            }
        } else {
            HiLogHelper.error(TAG, "Audio file not picked up");
        }
    }

    private void initAudio(Uri uri) {
        mUri = uri;

        // AudioWife takes care of click
        // handler for play/pause button
        AudioWife.getInstance()
                .init(mContext, uri)
                .setPlayView(mPlayMedia)
                .setPauseView(mPauseMedia)
                .setSeekBar(mMediaSeekBar)
                .setRuntimeView(mRunTime)
                .setTotalTimeView(mTotalTime);

        AudioWife.getInstance().addOnCompletionListener(new Player.IPlayerCallback() {
            @Override
            public void onPrepared() {
            }

            @Override
            public void onMessage(int i, int i1) {

            }

            @Override
            public void onError(int i, int i1) {

            }

            @Override
            public void onResolutionChanged(int i, int i1) {

            }

            @Override
            public void onPlayBackComplete() {
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        new ToastDialog(getContext()).setText("Completed").show();
                    }
                });
            }

            @Override
            public void onRewindToComplete() {

            }

            @Override
            public void onBufferingChange(int i) {
            }

            @Override
            public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

            }

            @Override
            public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

            }
        });

        AudioWife.getInstance().addOnPlayClickListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(getContext()).setText("Play").show();
            }
        });

        AudioWife.getInstance().addOnPauseClickListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(getContext()).setText("Pause").show();
            }
        });
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        resetView();
    }

    private void resetView() {
        mMediaSeekBar.setProgressValue(0);
        mRunTime.setText("00:00");
        mTotalTime.setText("00:00");
        mPlayMedia.setVisibility(Component.VISIBLE);
        mPauseMedia.setVisibility(Component.HIDE);
    }
}
